package max.ubitest.pipe.locators
{
	import flash.geom.Point;
	import flash.utils.Dictionary;
	
	import max.ubitest.pipe.Config;
	import max.ubitest.pipe.data.CellData;
	
	import mx.collections.ArrayCollection;
	
	import org.robotlegs.mvcs.Actor;
	
	public class CellLocator extends Actor
	{
		public var cells:ArrayCollection;
		
		public function CellLocator()
		{
			super();
			init();
		}
		
		private function init():void{
			cells = new ArrayCollection();
			for(var i:int = 0;i<36;i++){
				var c:CellData = new CellData(
					{
						x:i%Config.MAP_SIZE,
						y:int(i/Config.MAP_SIZE)
					}
				);
				cells.addItem(c);
			}
		}
		
		public function getCellsAround(cell:CellData):Vector.<CellData>{
			var v:Vector.<CellData> = new Vector.<CellData>();
			var index:int;
			for(var i:int = cell.x-1;i<=cell.x+1;i++){
				for(var j:int = cell.y - 1;j<=cell.y + 1;j++){
					if(i < 0 || j < 0 || i >= Config.MAP_SIZE || j >= Config.MAP_SIZE || (cell.x == i && cell.y == j)){
						//if out of map or is cell it self, continue
						continue;
					}
					index = i + j * Config.MAP_SIZE;
					v.push(cells.getItemAt(index) as CellData);
				}
			}
			return v;
		}
		
		public function getCellByPoint(p:Point):CellData{
			var index:int = p.x + p.y * Config.MAP_SIZE;
			if(index >= 0 && index < 36){
				return cells.getItemAt(index) as CellData;
			}else{
				return null;
			}
		}
	}
}